import { CancellationToken } from "./CancellationToken";

export class CancellationTokenSource {

    private IsCancelled: boolean;
    private CancelPromise: Promise<void>;
    private CancelResolver: () => void;

    constructor() {
        this.IsCancelled = false;
        this.CancelPromise = new Promise<void>((resolve) => {
            this.CancelResolver = resolve;
        });
    }

    get Token(): CancellationToken {
        return new CancellationToken(this.CancelPromise);
    }

    Cancel(): void {
        if (!this.IsCancelled) {
            this.IsCancelled = true;
            this.CancelResolver();
        }
    }
}
